package com.bytedance.common.a;

import android.net.TrafficStats;
import android.os.Build;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;

final class h implements Callable {
    private final String a;
    private final String b;
    private final c c;

    h(String arg1, String arg2, c arg3) {

        this.a = arg1;
        this.b = arg2;
        this.c = arg3;
    }

    private a a() {
        BufferedReader v2_1;
        a v0_5;
        Throwable v7;
        BufferedReader v3_1 = null;
        URLConnection v4;
        InputStream v3;
        BufferedReader v1_1;
        URLConnection v1_2;
        InputStream v4_1 = null;
        int v1;
        URLConnection v0_3 = null;
        a v2 = null;
        try {
            if (Build.VERSION.SDK_INT >= 14) {
                TrafficStats.setThreadStatsTag(40965);
            }

            String v0_2 = "http://203.107.1.1:80/" + this.b + "/d?host=" + this.a;
            if (HttpDns.a()) {
                HttpDns.a(v0_2);
            }

            v0_3 = new URL(v0_2).openConnection();
            v1 = 15000;
            ((HttpURLConnection) v0_3).setConnectTimeout(v1);
            ((HttpURLConnection) v0_3).setReadTimeout(15000);
            if (((HttpURLConnection) v0_3).getResponseCode() != 200) {
                HttpDns.b("response code is " + ((HttpURLConnection) v0_3).getResponseCode() + " expect 200");
                v3_1 = null;
                v4_1 = null;
            } else {
                v4_1 = ((HttpURLConnection) v0_3).getInputStream();
                v3_1 = new BufferedReader(new InputStreamReader(v4_1, "UTF-8"));
                StringBuilder v5 = new StringBuilder();
                while (true) {
                    String v1_5 = v3_1.readLine();
                    if (v1_5 == null) {
                        break;
                    }

                    v5.append(v1_5);
                }

                HttpDns.a("resolve host: " + this.a + ", return: " + v5.toString());
                v2 = new a(v5.toString(), this.c.f);
            }

            v0_5 = v2;
            a v1_6 = v0_5 == null ? new a(this.a, 300, this.c.f) : v0_5;
            if (this.c.a.size() < 100) {
                c v2_2 = this.c;
                String v3_2 = this.a;
                try {
                    Object v0_6 = v2_2.a().get(v3_2);
                    if (v0_6 == null) {
//                    goto label_66;
                    } else {
                        ((a) v0_6).a();
                    }
                } catch (Throwable v0) {
                }

                v2_2.a().put(v3_2, v1_6);
            } else {
                HttpDns.a("the total number of hosts is exceed 100");
            }

            this.c.c.remove(this.a);
            return v1_6;
        } catch (Throwable v1_3) {
            v1_3.printStackTrace();
        } finally {
            if (v0_3 != null) {
                ((HttpURLConnection) v0_3).disconnect();
            }
            if (v4_1 != null) {
                try {
                    v4_1.close();
                    label_44:
                    if (v3_1 != null) {
                        v3_1.close();
                    }
                } catch (IOException v0_4) {
                    HttpDns.a(((Exception) v0_4));
                    v0_5 = v2;
                }
            }

        }
        return null;
    }

    public final Object call() throws Exception {
        return this.a();
    }
}

